package com.wang.vedio.fragment;

import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxui.fragment.NewLazyFragment;
import com.wang.vedio.MainActivity2;
import com.wang.vedio.R;
import com.wang.vedio.fragment.index.TuiJianFragment;
import com.wang.vedio.fragment.index.ZuiReFragment;
import com.wang.vedio.fragment.index.ZuiXinFragment;
import com.wang.vedio.net.entity.LoginBean;
import com.wang.vedio.player.InviteFriendActivity;
import com.wang.vedio.view.StatusBarUtil;
import com.wang.vedio.widget.FragmentAda;
import com.wang.vedio.widget.MyViewPager;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 描述：添加类的描述
 *
 *
 * @time 2020/4/3
 */
public class MainFragment extends NewLazyFragment {
    Unbinder unbinder;

    @BindView(R.id.titleLinear)
    View titleLinear;
    @BindView(R.id.stl_main)
    SlidingTabLayout stlMain;
    @BindView(R.id.main_viewpager)
    MyViewPager mViewPager;

    @BindView(R.id.qianDaoBtn)
    View qianDaoBtn;
    @BindView(R.id.hasQianDaoTiShi)
    View hasQianDaoTiShi;



    private String[] mTitle = new String[]{"推荐", "最新", "最热"};
    private ArrayList<Fragment> fragList = new ArrayList<Fragment>();
    private int selectIndex = 0;

    @Override
    protected int getContentViewId() {
        return R.layout.gyss_fragment_main_layout1;
    }

    @Override
    protected void initView(View view) {
        unbinder = ButterKnife.bind(this, view);
        init();
        titleLinear.setPadding(0, StatusBarUtil.getStatusBarHeight(mContext), 0, 0);
        super.initView(view);
    }


    public void setCouldClick(boolean show){
        hasQianDaoTiShi.setVisibility(show?View.VISIBLE:View.GONE);
    }

    private void init() {
        fragList.clear();
        TuiJianFragment mnxFrag = new TuiJianFragment();
        fragList.add(mnxFrag);
        ZuiXinFragment workInfoFrag = new ZuiXinFragment();
        fragList.add(workInfoFrag);
        ZuiReFragment albbFrag = new ZuiReFragment();
        fragList.add(albbFrag);

        stlMain.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                selectIndex = position;
                for (int i = 0; i < fragList.size(); i++) {
                    if (i == position) {
                        stlMain.getTitleView(position).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        stlMain.getTitleView(i).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }

            }

            @Override
            public void onTabReselect(int position) {

            }
        });
        mViewPager.setAdapter(new FragmentAda(getChildFragmentManager(), fragList, null));
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectIndex = position;
                for (int i = 0; i < fragList.size(); i++) {
                    if (i == position) {
                        stlMain.getTitleView(position).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                    } else {
                        stlMain.getTitleView(i).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                    }
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        stlMain.setViewPager(mViewPager, mTitle);

        mViewPager.setCurrentItem(0);
        stlMain.getTitleView(selectIndex).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    }

    private LoginBean user;

    @Override
    protected void initData() {
    }

    @OnClick({R.id.qianDaoBtn})
    public void OnClick(View v){
        if(v.getId() == R.id.qianDaoBtn){
            Bundle bundle = new Bundle();
            bundle.putInt("type",0);
            RxActivityTool.skipActivity(mContext, InviteFriendActivity.class,bundle);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        setCouldClick(((MainActivity2)getActivity()).isCanSign());
    }
}
